<template>
  <view class="cubeNavBox" :style="[outModuleCss]">
    <view :style="[moduleBgCss]" class="module-bg"></view>
    <view class="cubeNavContent">
      <!-- 单列滑动 -->
      <view class="style1" v-if="propsData.params.drain == 2">
        <scroll-view
          scroll-x
          class="scrollView"
          :show-scrollbar="false"
          style="width: 100%"
          @scroll="scrollLeft"
        >
          <view class="pageList">
            <view
              class="pageItem"
              v-for="(page, pageIndex) in pages"
              :key="pageIndex"
            >
              <view class="navList">
                <view
                  class="navBox"
                  v-for="(item, index) in page"
                  :style="{
                    width:
                      propsData.params.columnNumber == 3
                        ? '33.3%'
                        : propsData.params.columnNumber == 4
                        ? '25%'
                        : propsData.params.columnNumber == 5
                        ? '20%'
                        : ''
                  }"
                  :key="index"
                >
                  <button
                    class="navItem"
                    @click.stop="changeNav(item)"
                    :open-type="
                      item.link.open_type == 'contact' ? 'contact' : ''
                    "
                  >
                    <image
                      lazy-load
                      :src="item.iconUrl"
                      alt=""
                      mode="aspectFill"
                    />
                    <text :style="[item.css]">{{ item.name }}</text>
                  </button>
                </view>
              </view>
            </view>
          </view>
        </scroll-view>
        <view class="scroll" v-if="scrollWidth > 0">
          <text :style="'left:' + (scroll / scrollWidth) * 100 + '%;'"></text>
        </view>
      </view>

      <!-- 整页滑动 -->
      <view class="style2" v-if="propsData.params.drain == 1">
        <swiper
          :style="[
            {
              height:
                propsData.params.numberOfLines == 1
                  ? '172rpx'
                  : propsData.params.numberOfLines == 2
                  ? '342rpx'
                  : '',
              width: '100%'
            }
          ]"
          @change="changePage"
        >
          <swiper-item
            class="pageItem"
            v-for="(page, pageIndex) in pages"
            :key="pageIndex"
          >
            <view class="navList">
              <view
                class="navBox"
                v-for="(item, index) in page"
                :style="{
                  width:
                    propsData.params.columnNumber == 3
                      ? '33.3%'
                      : propsData.params.columnNumber == 4
                      ? '25%'
                      : propsData.params.columnNumber == 5
                      ? '20%'
                      : ''
                }"
                :key="index"
              >
                <button
                  class="navItem"
                  @click.stop="changeNav(item)"
                  :open-type="item.link.open_type == 'contact' ? 'contact' : ''"
                >
                  <image lazy-load :src="item.iconUrl" alt="" mode="aspectFill" />
                  <text :style="[item.css]">{{ item.name }}</text>
                </button>
              </view>
            </view>
          </swiper-item>
        </swiper>
        <view class="scroll">
          <text
            :style="[
              {
                left: (current / pages.length) * 100 + '%',
                width: 80 / pages.length + 'rpx'
              }
            ]"
          ></text>
        </view>
      </view>

      <!-- 固定显示 -->
      <view class="style3" v-if="propsData.params.drain == 3">
        <view class="pageList">
          <view
            class="pageItem"
            v-for="(page, pageIndex) in pages"
            :key="pageIndex"
          >
            <view class="navList">
              <view
                class="navBox"
                v-for="(item, index) in page"
                :style="{
                  width:
                    propsData.params.columnNumber == 3
                      ? '33.3%'
                      : propsData.params.columnNumber == 4
                      ? '25%'
                      : propsData.params.columnNumber == 5
                      ? '20%'
                      : ''
                }"
                :key="index"
              >
                <button
                  class="navItem"
                  @click.stop="changeNav(item)"
                  :open-type="item.link.open_type == 'contact' ? 'contact' : ''"
                >
                  <image lazy-load :src="item.iconUrl" alt="" mode="aspectFill" />
                  <text :style="[item.css]">{{ item.name }}</text>
                </button>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
function group(array, subGroupLength) {
  let index = 0;
  let newArray = [];
  while (index < array.length) {
    newArray.push(array.slice(index, (index += subGroupLength)));
  }
  return newArray;
}
import { diyMixin } from "../mixins/diy";
export default {
  props: {
    propsData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      // 可滚动宽度
      scrollWidth: 0,
      // 当前滑动位置
      scroll: 0,
      // 当前轮播图所在索引
      current: 0
    };
  },
  mixins: [diyMixin],
  mounted() {
    if (this.propsData.params.drain == 2) {
      this.$nextTick(() => {
        uni
          .createSelectorQuery()
          .in(this)
          .select(".scrollView")
          .fields(
            {
              size: true,
              scrollOffset: true
            },
            (data) => {
              if (data.scrollWidth > data.width) {
                this.scrollWidth = data.scrollWidth;
              }
            }
          )
          .exec();
      });
    }
  },
  computed: {
    pages() {
      let count = Math.ceil(
        this.propsData.params.list.length /
          (this.propsData.params.columnNumber *
            this.propsData.params.numberOfLines)
      );
      if (count > 0) {
        return group(
          this.propsData.params.list,
          this.propsData.params.columnNumber *
            this.propsData.params.numberOfLines
        );
      } else {
        return [];
      }
    }
  },
  methods: {
    changeNav(e) {
      if (e.link) {
        this.$utils.diySkip(e.link);
      }
    },
    scrollLeft(e) {
      this.scroll = e.detail.scrollLeft;
    },
    changePage(e) {
      this.current = e.detail.current;
    }
  }
};
</script>

<style lang="scss" scoped>
.cubeNavBox {
  width: 100%;
  position: relative;
  .cubeNavContent {
    padding: 20rpx 20rpx 0 20rpx;
  }

  .navList {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .navBox {
      display: flex;
      align-items: center;
      justify-content: center;
      padding-bottom: 40rpx;
    }
    .navItem {
      display: flex;
      flex-direction: column;
      align-items: center;
      position: relative;
      height: auto;
      image {
        width: 80rpx;
        height: 80rpx;
      }
      text {
        white-space: nowrap;
        margin-top: 12rpx;
        // width: 100%;
        // white-space: nowrap;
        // overflow: hidden;
        // text-align: center;
        // text-overflow: ellipsis;
      }
    }
  }
}
.style1 {
  position: relative;
  width: 100%;
  /deep/.uni-scroll-view::-webkit-scrollbar {
    /* 隐藏滚动条，但依旧具备可以滚动的功能 */
    display: none;
  }
  .scroll {
    position: absolute;
    bottom: 20rpx;
    left: 50%;
    transform: translateX(-50%);
    width: 80rpx;
    height: 6rpx;
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 3rpx;
    overflow: hidden;
    text {
      width: 40rpx;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      background-color: #262626;
      border-radius: 3rpx;
      z-index: 100;
    }
  }
  .pageList {
    width: 100%;
    display: flex;
    align-items: stretch;
    flex-wrap: nowrap;
    .pageItem {
      width: 100%;
      min-width: 100%;
    }
  }
}
.style2 {
  width: 100%;
  // z-index: 10000;
  .scroll {
    position: absolute;
    bottom: 20rpx;
    left: 50%;
    transform: translateX(-50%);
    width: 80rpx;
    height: 6rpx;
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 3rpx;
    overflow: hidden;
    text {
      width: 40rpx;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      background-color: #262626;
      border-radius: 3rpx;
      z-index: 100;
    }
  }
}
.style3 {
  .pageList {
    width: 100%;
    .pageItem {
      width: 100%;
      min-width: 100%;
    }
  }
}
.module-bg {
  width: 100%;
  height: 100%;
  z-index: -1;
  position: absolute;
  left: 0;
  top: 0;
}
.style1::-webkit-scrollbar {
  display: none;
}

button {
  padding: 0;
  margin: 0;
  background: none;
  line-height: 1.2;
  font-size: 30rpx;
  color: transparent;

  &::after {
    border: 0;
  }
}
</style>
